package recharge_management

import (
	"context"

	"github.com/spf13/cast"

	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"

	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/admin-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UsdtListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// usdt--列表
func NewUsdtListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UsdtListLogic {
	return &UsdtListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UsdtListLogic) UsdtList(req *types.UsdtListRequest) (resp *types.UsdtListResponse, err error) {
	rpcReq := rpc.UsdtListRequest{
		UsdtType: req.UsdtType,
		Page: &rpc.Page{
			Page:     req.Page,
			PageSize: req.Size,
		},
	}

	rpcResp, err := l.svcCtx.AssetsRpc.TenantChannel.UsdtList(l.ctx, &rpcReq)
	if err != nil {
		return nil, err
	}

	usdtList := make([]types.Usdt, len(rpcResp.Records))
	for i, record := range rpcResp.Records {
		usdtList[i] = types.Usdt{
			Id:        record.Id,
			CreatedAt: record.CreatedAt,
			UpdatedAt: record.UpdatedAt,
			Name:      record.Name,
			UsdtType:  record.UsdtType,
			QrCode:    record.QrCode,
			Address:   record.Address,
			Sort:      cast.ToInt64(record.Sort),
			Status:    int32(record.Status),
		}
	}

	return &types.UsdtListResponse{
		Records: usdtList,
		Pagination: types.Pagination{
			PageSize:  int32(rpcResp.Pagination.PageSize),
			Page:      int32(rpcResp.Pagination.Page),
			Total:     rpcResp.Pagination.Total,
			TotalPage: rpcResp.Pagination.TotalPage,
		},
	}, nil
}
